package org.enhance.common.model.po;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class GeoLine extends AbstractGeo {

	private static final long serialVersionUID = -110852318903776599L;

	private List<GeoPoint> geoPoints;

	public GeoLine addGeoPoint(BigDecimal longitude, BigDecimal latitude) {
		GeoPoint geoPoint = new GeoPoint(longitude, latitude);
		addGeoPoint(geoPoint);
		return this;
	}

	public GeoLine addGeoPoint(BigDecimal longitude, BigDecimal latitude, BigDecimal altitude) {
		GeoPoint geoPoint = new GeoPoint(longitude, latitude, altitude);
		addGeoPoint(geoPoint);
		return this;
	}

	public GeoLine addGeoPoint(GeoPoint geoPoint) {
		if (null != geoPoint) {
			if (null == geoPoints) {
				geoPoints = new ArrayList<>();
			}
			geoPoints.add(geoPoint);
		}
		return this;
	}

	@Override
	public String generateShape() {
		if (!geoPoints.isEmpty()) {
			StringBuilder sb = new StringBuilder("LINESTRING(");
			for (int i = 0; i < geoPoints.size(); i++) {
				GeoPoint geoPoint = geoPoints.get(i);
				sb.append(geoPoint.getLongitude()).append(" ").append(geoPoint.getLatitude());
				if (geoPoint.getAltitude() != null) {
					sb.append(" ").append(geoPoint.getAltitude());
				}
				if (i != geoPoints.size() - 1) {
					sb.append(",");
				}
			}
			sb.append(")");
			return sb.toString();
		}
		return null;
	}
}
